package com.zhiwang.planet.ui.loginv2

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProviders
import com.zhiwang.planet.BuildConfig
import com.zhiwang.planet.R
import com.zhiwang.planet.databinding.ActivityLoginV2Binding
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.event.WxLoginEvent
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.mvvm.viewmodel.loginv2.LoginViewModel
import com.zhiwang.planet.mvvm.viewmodel.loginv2.RegisterViewModel
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.util.*
import kotlinx.android.synthetic.main.activity_login_v2.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import javax.inject.Inject

class LoginV2Activity : BaseActivity() {


    @Inject
    lateinit var restApi: RestApi

    lateinit var loginViewModel: LoginViewModel

    lateinit var registerViewModel: RegisterViewModel

    val TAG = "LoginV2Activity"


    var deviceId: String? = null

    companion object {

        fun start(context: Context) {
            context.startActivity(Intent(context, LoginV2Activity::class.java))
        }
    }

    override fun initInjection() {
        super.initInjection()
        DaggerUserComponent.builder()
                .appComponent(getAppComponent())
                .accountModule(AccountModule(this))
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)
        EventBus.getDefault().register(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil.setContentView<ActivityLoginV2Binding>(this, R.layout.activity_login_v2)
        setSupportActionBar(toolbar)
        Utils.setStatusBar(this, false, false)
        loginViewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java)
        registerViewModel = ViewModelProviders.of(this).get(RegisterViewModel::class.java)
        registerViewModel.context = this
        loginViewModel.restApi = restApi
        loginViewModel.context = this
        loginViewModel.errorHandler = this::onError
        binding.viewModel = loginViewModel
        binding.vm = registerViewModel
        binding.lifecycleOwner = this
//        loginViewModel.loadQQ()
        AppUtil.getDeviceId {
            deviceId = it
            if (it != null) {
                loginViewModel.deviceId = it
                runOnUiThread {
//                    deviceid_paper.text = "设备码:$it"
                }
                TokenManager.saveDevice(it)

            }
        }

        if (AppHelper.isDev()) {
            toolbar.setOnLongClickListener {
                val env = AppHelper.switchEnv()
                if (env) {
                    Toaster.message("已经切换到测试环境，关闭app 后重进后即可生效")
                } else {
                    Toaster.message("已经切换到外网线上环境，关闭app 后重进后即可生效")
                }
                TokenManager.clearToken()
                return@setOnLongClickListener true
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = false)
    fun onWxLogin(e: WxLoginEvent) {
        loginViewModel.tryWxLogin(e.code)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}